﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BellaCode.Collections.ObjectModel;
using ResidencyPlacement;

namespace ResidencyPlacementWPF
{
    /// <summary>
    /// Interaction logic for ViewApplications.xaml
    /// </summary>
    public partial class ViewStudentApplications : Window
    {
        private Student student;
        private ObservableHashSet<ResidencyPlacement.Application> applications;

        public ViewStudentApplications()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource applicationViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("applicationViewSource")));
            applicationViewSource.Source = applications;
        }

        private void addApplicationButton_Click(object sender, RoutedEventArgs e)
        {
            AddApplication addApplication = new AddApplication();
            addApplication.Owner = this;
            addApplication.Title = "Add Application for " + student.Name;
            addApplication.comboBox1.ItemsSource = App.ResSystem.Hospitals;
            addApplication.ShowDialog();

            if (addApplication.DialogResult == true)
            {
                try
                {
                    var selectedHospital = (Hospital)addApplication.comboBox1.SelectedItem;
                    var newApp = new ResidencyPlacement.Application();
                    newApp.Hospital = selectedHospital.ID;
                    App.ResSystem.StudentAppliesToAHospital(student, selectedHospital, newApp);
                }
                catch
                {

                }
            }
        }

        private void acceptButton_Click(object sender, RoutedEventArgs e)
        {
            var selected = (ResidencyPlacement.Application)(applicationDataGrid.SelectedItem);
            Hospital hospital = App.ResSystem.Hospitals.First(h => h.ID == selected.Hospital);

            try
            {
                App.ResSystem.StudentRepliesToOffer(student, hospital, true);
                applicationDataGrid.Items.Refresh();
                applicationDataGrid.SelectedItem = null;
            }
            catch
            {

            }
        }

        private void declineButton_Click(object sender, RoutedEventArgs e)
        {
            var selected = (ResidencyPlacement.Application)(applicationDataGrid.SelectedItem);
            Hospital hospital = App.ResSystem.Hospitals.First(h => h.ID == selected.Hospital);

            try
            {
                App.ResSystem.StudentRepliesToOffer(student, hospital, false);
                applicationDataGrid.Items.Refresh();
                applicationDataGrid.SelectedItem = null;
            }
            catch
            {

            }
        }

        public ObservableHashSet<ResidencyPlacement.Application> Applications
        {
            set
            {
                applications = value;
            }
        }

        public Student Student
        {
            get
            {
                return this.student;
            }

            set
            {
                student = value;
            }
        }

        private void applicationDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            if (dataGrid.SelectedItem != null && !applications.Any(a => a.Status == ApplicationStatus.OfferAccepted))
            {
                var selectedApp = (ResidencyPlacement.Application)dataGrid.SelectedItem;
                if (selectedApp.Status == ApplicationStatus.HasBeenSelected)
                {
                    acceptButton.IsEnabled = true;
                    declineButton.IsEnabled = true;
                }
                else
                {
                    acceptButton.IsEnabled = false;
                    declineButton.IsEnabled = false;  
                }
            }
            else
            {
                var selectedApp = (ResidencyPlacement.Application)dataGrid.SelectedItem;
                if (selectedApp.Status == ApplicationStatus.HasBeenSelected)
                {
                    acceptButton.IsEnabled = false;
                    declineButton.IsEnabled = true;
                }
                else
                {
                    acceptButton.IsEnabled = false;
                    declineButton.IsEnabled = false;
                }
            }
        }
    }
}
